<template>
  <a-modal
    title="编辑库存表"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item v-show="false"><a-input v-decorator="['id']" /></a-form-item>
        <a-form-item
          label="库存编号"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入库存编号" v-decorator="['stockCode', {rules: [{required: true, message: '请输入库存编号！'}]}]" />
        </a-form-item>
        <a-form-item
          label="物品名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
        <a-select :default-value="matList[0].id" style="width: 120px" @change="handleChange" v-decorator="['matId', {rules: [{required: false, message: '请输入数量！'}]}]">
          <a-select-option :key="index" v-for="(item,index) in matList" :value="item.id">
            {{ item.matName }}
          </a-select-option>
        </a-select>
        </a-form-item>
        <a-form-item
          label="数量"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入数量" v-decorator="['stockQty', {rules: [{required: true, message: '请输入数量！'}]}]" />
        </a-form-item>
        <a-form-item
          label="状态"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入状态" v-decorator="['status', {rules: [{required: true, message: '请输入状态！'}]}]" />
        </a-form-item>
        <a-form-item
          label="备注"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入备注" v-decorator="['remark']" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
  import { bizStockEdit } from '@/api/modular/main/system/bizStockManage'
  export default {
    props: {
      matList: {
        type: Array,
        default: () => []
      }
    },
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        visible: false,
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      edit (record) {
        this.visible = true
        setTimeout(() => {
          this.form.setFieldsValue(
            {
              id: record.id,
              stockCode: record.stockCode,
              matId: record.matId,
              stockQty: record.stockQty,
              status: record.status,
              remark: record.remark,
              createUserId: record.createUserId
            }
          )
        }, 100)
      },
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object' && values[key] != null) {
                values[key] = JSON.stringify(values[key])
              }
            }
            bizStockEdit(values).then((res) => {
              if (res.success) {
                this.$message.success('编辑成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error('编辑失败')//  + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
